﻿#include <iostream>
using namespace std;

//961.在长度2N的数组中找出重复N次的元素
//https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/
class Solution {
public:
    int repeatedNTimes(vector<int>& nums) { //map实现
        map<int, int> m;
        int n = nums.size() / 2;
        for (const auto& x : nums)
        {
            ++m[x];
            if (m[x] == n)
                return x;
        }
        return -1;
    }
};

//217.存在重复元素
//https://leetcode.cn/problems/contains-duplicate/
class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        map<int, int> m;
        for (const auto& x : nums)
        {
            ++m[x];
        }
        for (const auto& x : m)
        {
            if (x.second >= 2)
                return true;
        }
        return false;
    }
};

//884.两句话中的不常见单词
//https://leetcode.cn/problems/uncommon-words-from-two-sentences/submissions/
class Solution {
public:
    vector<string> uncommonFromSentences(string s1, string s2) {
        vector<string> ret;
        vector<string> tmp;
        string str;
        s1.push_back(' ');
        s1 += s2;
        for (const auto& s : s1)
        {
            if (s == ' ')
            {
                tmp.push_back(str);
                str.clear();
                continue;
            }
            str.push_back(s);
        }
        tmp.push_back(str);
        map<string, int> m;
        for (const auto& x : tmp)
        {
            ++m[x];
        }
        for (const auto& x : m)
        {
            if (x.second == 1)
                ret.push_back(x.first);
        }
        return ret;
    }
};